using System;
using System.Collections.Generic;
using UnityEngine;
using Random = UnityEngine.Random;

namespace SimpleCore
{
    public static class CommonTool
    {
        public static bool IsRandomPass(int percentRate)
        {
            return UnityEngine.Random.Range(0, 100) <= percentRate;
        }

        public static int RandomIndex(params int[] weights)
        {
            if (weights.Length <= 0) return -1;
            int total = 0;
            int[] temp = new int[weights.Length];
            for (int i = 0; i < weights.Length; i++)
            {
                total += weights[i];
                temp[i] = total;
            }
            
            //TODO:改成二分查找
            var randomValue = Random.Range(0, total);
            for (int i = 0; i < temp.Length; i++)
            {
                if (temp[i] > randomValue)
                {
                    return i;
                }
            }

            return weights.Length - 1;
        }
        
        public static string GetTypeName<T>()
        {
            return typeof(T).Name;
        }

        /// <summary>
        /// 仅仅用于空列表返回，避免多次创建
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static List<T> GetEmptyList<T>()
        {
            var key = typeof(T);
            if (_emptyListMap.TryGetValue(key, out var data))
            {
                var list = data as List<T>;
                list.Clear();
                return list;
            }
            else
            {
                var list = new List<T>();
                _emptyListMap[key] = list as List<object>;
                return list;
            }
        }

        public static void DeepCopyList<T>(List<T> source, List<T> target, bool isClearTarget, int count = 0)
        {
            if (isClearTarget)
            {
                target.Clear();
            }

            if (count == 0 || count > source.Count)
            {
                count = source.Count;
            }

            for (int i = 0; i < count; i++)
            {
                target.Add(source[i]);
            }
        }
        
        public static void DeepCopyList<T>(T[] source, List<T> target, bool isClearTarget, int count = 0)
        {
            if (isClearTarget)
            {
                target.Clear();
            }

            if (count == 0 || count > source.Length)
            {
                count = source.Length;
            }

            for (int i = 0; i < count; i++)
            {
                target.Add(source[i]);
            }
        }

        public static void RemoveDefaultElement<T>(List<T> list)
        {
            for (int i = list.Count - 1; i >= 0; i--)
            {
                if (list[i].Equals(default(T)))
                {
                    list.RemoveAt(i);
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="list"></param>
        /// <param name="condition">true则删除</param>
        /// <typeparam name="T"></typeparam>
        public static void RemoveElementByCondition<T>(List<T> list, Func<T, bool> condition)
        {
            for (int i = list.Count - 1; i >= 0; i--)
            {
                if (condition?.Invoke(list[i]) ?? false)
                {
                    list.RemoveAt(i);
                }
            }
        }
        
        /// <summary>
        /// 递归查找Child
        /// </summary>
        /// <param name="root"></param>
        /// <param name="childName"></param>
        /// <returns></returns>
        public static Transform FindChild(Transform root, string childName)
        {
            var childCount = root.childCount;
            for (int i = 0; i < childCount; i++)
            {
                var child = root.GetChild(i);
                if (child.name == childName)
                {
                    return child;
                }

                return FindChild(child, childName);
            }

            return null;
        }

        static Dictionary<Type, List<object>> _emptyListMap = new Dictionary<Type, List<object>>();
    }
}